package kyssion.leetcode.num51_100;

import kyssion.leetcode.util.TreeNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class code95_不同的二叉搜索树 {

    public static void main(String[] args) {
        List<TreeNode> list = new code95_不同的二叉搜索树().generateTrees(3);
    }

    public List<TreeNode> generateTrees(int n) {
        if(n==0){
            return new ArrayList<>();
        }
        return ans(1, n);
    }

    public List<TreeNode> ans(int start, int end) {
        if (start > end) {
            return new ArrayList<>(Collections.singletonList(null));
        }
        if (start == end) {
            return new ArrayList<>(Collections.singletonList(new TreeNode(start)));
        }
        List<TreeNode> treeNodes = new ArrayList<>();
        for (int a = start; a <= end; a++) {
            List<TreeNode> left = ans(start, a - 1);
            List<TreeNode> right = ans(a + 1, end);
            for (TreeNode l : left) {
                for (TreeNode r : right) {
                    TreeNode base = new TreeNode(a);
                    base.left = l;
                    base.right = r;
                    treeNodes.add(base);
                }
            }
        }
        return treeNodes;
    }
}
